<?php
namespace Common\Model;

use Think\Model;

class ImageModel extends  CommonModel {

	public $tableName = 'image';
	//状态
	public $status = [
		1 => '启用',
		2 => '禁用',
	];

	//查询渠道列表
	public function get_image_list(array $params,$field="a.*"){
		$where = [];
		$pagesize = empty($params['pageSize']) ? '20' : $params['pageSize'];
		$params['status']?$where['a.status'] = $params['status']:"";
		$count = $this
			->alias("a")
			->where($where)
			->count();
		$Page = new \Think\Page($count, $pagesize);
		$limit = $Page->firstRow . ',' . $Page->listRows;

		$list = $this
			->alias("a")
			->field($field)
			->where($where)
			->order("a.id desc")
			->limit($limit)
			->select();
		int_to_string($list,['status'=>$this->status]);
		$data = [
			'count' => $count,
			'list'  => $list
		];
		return $data;
	}




	//添加/修改渠道
	public function image_save(array $params){
		$img = "";
		if($params['image'][0]['url']){
			$imgs = explode("Uploads",$params['image'][0]['url']);
			$img = "/Uploads".$imgs[1];
		}
		$data = [
			'image'       => $img,
			'status'      => $params['status'],
		];

		if ($params['image_id']) {
			$data['update_time'] = time();
			$data['update_by'] = $params['userId'];
			$result = $this->where(['id' => $params['image_id']])->save($data);
		} else {
 			$data['create_time'] = time();
			$data['create_by'] = $params['userId'];
			$result = $this->add($data);
		}
		$id = $params['image_id']?$params['image_id']:$result;
		$this->where(['id' => ['neq',$id]])->save(['status' => 2]);
		return $this->returnData($result?"设置成功":"设置失败",$result?200:500);
	}



	//得到套餐详情
	public function get_detail_list(array $params){
		$where = [];
		$params['menu_ids']?$where['a.menu_id'] = ["in",$params['menu_ids']]:"";
		$model = M("MenuDetail");
		$list = $model
		->alias("a")
		->field("a.*")
		->where($where)
		->order("a.id asc")
		->select();
		return $list;
	}
}

?>
